package org.example.system.service.impl;

import lombok.RequiredArgsConstructor;
import java.lang.management.ManagementFactory;
import org.example.system.service.SystemInfoService;
import org.springframework.stereotype.Service;
import com.sun.management.OperatingSystemMXBean;
import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class SystemInfoServiceImpl implements SystemInfoService {

    private final OperatingSystemMXBean osBean;

    public SystemInfoServiceImpl() {
        // 获取操作系统的 MXBean
        this.osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    }

    /**
     * 获取系统CPU信息
     *
     * @return 系统CPU信息
     */
    @Override
    public Map<String, Object> getCpuInfo() {
        Map<String, Object> cpuMap = new HashMap<>();
        cpuMap.put("availableProcessors", osBean.getAvailableProcessors());

        double cpuLoad = osBean.getCpuLoad();
        if (cpuLoad >= 0) {
            cpuMap.put("cpuUsage", cpuLoad * 100);
        } else {
            cpuMap.put("cpuUsage", -1); // 表示无法获取CPU负载
        }

        double systemLoadAverage = osBean.getSystemLoadAverage();
        if (systemLoadAverage >= 0) {
            cpuMap.put("systemLoadAverage", systemLoadAverage);
        } else {
            cpuMap.put("systemLoadAverage", -1); // 表示无法获取系统负载平均值
        }

        return cpuMap;
    }

    /**
     * 获取内存信息
     *
     * @return 内存信息
     */
    @Override
    public Map<String, Object> getMemoryInfo() {
        Map<String, Object> memoryMap = new HashMap<>();
        long totalMemorySize = osBean.getTotalMemorySize();
        long freeMemorySize = osBean.getFreeMemorySize();
        long usedMemorySize = totalMemorySize - freeMemorySize;

        // 保留精度，使用double类型
        memoryMap.put("totalMemory", String.format("%.2f", totalMemorySize / 1e9));
        memoryMap.put("usedMemory", String.format("%.2f", usedMemorySize / 1e9));
        memoryMap.put("freeMemory", String.format("%.2f", freeMemorySize / 1e9));

        // 避免除零异常
        if (totalMemorySize > 0) {
            memoryMap.put("memoryUsage", (double) usedMemorySize / totalMemorySize * 100);
        } else {
            memoryMap.put("memoryUsage", 0.0);
        }

        return memoryMap;
    }


    /**
     * 获取磁盘信息
     *
     * @return 磁盘信息
     */
    @Override
    public Map<String, Object> getDiskInfo() {
        Map<String, Object> diskMap = new HashMap<>();
        File[] roots = File.listRoots();
        for (File root : roots) {
            Map<String, Object> diskInfo = getDiskInfoForRoot(root);
            diskMap.put(root.getAbsolutePath(), diskInfo);
        }
        return diskMap;
    }

    /**
     * 获取单个磁盘根目录的磁盘信息
     *
     * @param root 磁盘根目录
     * @return 磁盘信息
     */
    private Map<String, Object> getDiskInfoForRoot(File root) {
        Map<String, Object> diskInfo = new HashMap<>();
        long totalSpace = root.getTotalSpace();
        long freeSpace = root.getFreeSpace();
        long usedSpace = totalSpace - freeSpace;

        diskInfo.put("totalSpace", totalSpace / 1_000_000_000L); // 转换为GB
        diskInfo.put("usedSpace", usedSpace / 1_000_000_000L); // 转换为GB
        diskInfo.put("freeSpace", freeSpace / 1_000_000_000L); // 转换为GB

        if (totalSpace > 0) {
            diskInfo.put("diskUsage", (int) ((double) usedSpace / totalSpace * 100));
        } else {
            diskInfo.put("diskUsage", 0); // 如果totalSpace为0，设置diskUsage为0
        }

        return diskInfo;
    }


    /**
     * 获取操作系统信息
     *
     * @return 操作系统信息
     */
    @Override
    public Map<String, Object> getOsInfo() {
        Map<String, Object> osMap = new HashMap<>();
        osMap.put("osName", System.getProperty("os.name", "Unknown"));
        osMap.put("osVersion", System.getProperty("os.version", "Unknown"));
        osMap.put("osArch", System.getProperty("os.arch", "Unknown"));
        return Collections.unmodifiableMap(osMap);
    }

}

